package cn.topkinson.web.a.component;

import cn.topkinson.api.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 黄子濠
 * @date 2020/8/30 21:50
 */
@Service
public class WebConsumer {

    ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

    @Reference
    private HelloService helloService;

    public String sayHello(String name,int timeToWait) {
        return helloService.sayHello(name,timeToWait);
    }

    TreeMap<String, String> treeMap = new TreeMap<>(Comparator.naturalOrder());

    public void startToCall(int times){
        int t = (int)(times/60);
        long start = System.currentTimeMillis();
        for (int j = 0;j<60;j++){
            try {
                Thread.sleep(1000);
                for (int i = 0;i<t;i++){
                    cachedThreadPool.execute(()->{
                        helloService.methodA("1");
                    });
                    cachedThreadPool.execute(()->{
                        helloService.methodB("1");
                    });
                    cachedThreadPool.execute(()->{
                        helloService.methodC("1");
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("耗时:["+(System.currentTimeMillis()-start)+"],对ABC方法各发出["+times+"]次调用");

    }

}
